// pages/goods_list/index.js
import {request} from "../../resquest/index.js"
import regeneratorRuntime from '../../lib/runtime/runtime';
Page({

  /**
   * 页面的初始数据
   */
  data: {
      tabList:[{
        id:0,
        value:'综合',
        active: true
      },{
        id:1,
        value:'销量',
        active: false
      },{
        id:2,
        value:'价格',
        active: false
      }],
      goodsList:[]
  },
  // 总页数
  countPage:0,
  // 子组件传递事件
  handleTabChange(e){
    const index = e.detail;
    let tabs = this.data.tabList;
    tabs.forEach((v,i) => i===index?v.active = true:v.active=false
    )
    this.setData({
      tabList:tabs
    })    
    
  },

  // 接口要的参数
  queryParams:{
    query:'',
    cid:'',
    pagenum:1,
    pagesize:10
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options);
    // 接收参数cid或者query
    this.queryParams.cid = options.cid ||'';
    this.queryParams.query = options.query ||'';
    this.getGoodList();
    
    
  },
  async getGoodList(){
    const {data:res} = await request({url:"/goods/search",data:this.queryParams})
    const total = res.message.total;
    this.countPage = Math.ceil(total/this.queryParams.pagesize);
    // console.log(res);
    this.setData({
      goodsList:[...this.data.goodsList,...res.message.goods]
    });
    // 关闭下拉刷新窗口,第一次直接关不会报错  
    wx.stopPullDownRefresh();
    // console.log(this.data.goodsList);
    
  },
  // 触底事件，上刷加载下一页
  onReachBottom(){
    // console.log(111);
    if(this.queryParams.pagenum>=this.countPage){
      wx.showToast({
        title: '没有更多数据了',
        icon: ''
      })
      
    }else{
      this.queryParams.pagenum++;
      this.getGoodList()
    }
    
  },
  onPullDownRefresh(){
    this.setData({
      goodsList:[]
    })
    this.queryParams.pagenum=1;
    this.getGoodList()
  }
})